<template>
  <div class="test-container">
    <h2>淘汰管理功能测试</h2>
    
    <el-card class="test-card">
      <template #header>
        <span>测试信息</span>
      </template>
      
      <el-form :model="testForm" label-width="120px">
        <el-form-item label="测试ID">
          <el-input v-model="testForm.id" placeholder="请输入要测试的ID" />
        </el-form-item>
        
        <el-form-item label="淘汰日期">
          <el-date-picker v-model="testForm.EliminationDate" type="date" placeholder="请选择淘汰日期" />
        </el-form-item>
        
        <el-form-item label="耳号">
          <el-input v-model="testForm.SheepFilesId" placeholder="请输入耳号" />
        </el-form-item>
        
        <el-form-item label="品种">
          <el-input v-model="testForm.Breed" placeholder="请输入品种" />
        </el-form-item>
        
        <el-form-item label="栋舍ID">
          <el-input-number v-model="testForm.House_typeId" :min="1" />
        </el-form-item>
        
        <el-form-item label="栏位">
          <el-input-number v-model="testForm.Field" :min="1" />
        </el-form-item>
        
        <el-form-item label="离场状态">
          <el-switch v-model="testForm.LeaveStatus" />
        </el-form-item>
        
        <el-form-item label="淘汰原因">
          <el-input v-model="testForm.EliminationReason" placeholder="请输入淘汰原因" />
        </el-form-item>
        
        <el-form-item label="处理措施ID">
          <el-input-number v-model="testForm.Handling_measuresId" :min="1" />
        </el-form-item>
        
        <el-form-item label="羊只类型ID">
          <el-input-number v-model="testForm.SheepTypeId" :min="1" />
        </el-form-item>
      </el-form>
      
      <div class="button-group">
        <el-button type="primary" @click="testGetDetail">测试获取详情</el-button>
        <el-button type="success" @click="testUpdate">测试更新</el-button>
        <el-button type="warning" @click="testAdd">测试新增</el-button>
      </div>
    </el-card>
    
    <el-card class="result-card">
      <template #header>
        <span>测试结果</span>
      </template>
      
      <div class="result-content">
        <h4>请求数据:</h4>
        <pre>{{ requestData }}</pre>
        
        <h4>响应结果:</h4>
        <pre>{{ responseData }}</pre>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Sheep_BreedingWrite } from '@/utils/Interceptors'

const testForm = reactive({
  id: '15',
  EliminationDate: '',
  SheepFilesId: 'SF001',
  Breed: '测试品种',
  House_typeId: 5,
  Field: 3,
  LeaveStatus: true,
  EliminationReason: '测试淘汰',
  Handling_measuresId: 2,
  SheepTypeId: 3
})

const requestData = ref('')
const responseData = ref('')

const testGetDetail = async () => {
  try {
    const url = `/api/CullingManagement/Detail/detail/${testForm.id}`
    console.log('获取详情URL:', url)
    
    const res = await Sheep_BreedingWrite.get(url)
    responseData.value = JSON.stringify(res.data, null, 2)
    
    if (res.data && res.data.code === 200) {
      ElMessage.success('获取详情成功')
      // 填充表单数据
      const data = res.data.data
      Object.assign(testForm, data)
    } else {
      ElMessage.error(res.data.msg || '获取详情失败')
    }
  } catch (error: any) {
    console.error('获取详情失败:', error)
    responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('获取详情失败')
  }
}

const testUpdate = async () => {
  try {
    const data = {
      Id: Number(testForm.id),
      EliminationDate: testForm.EliminationDate ? new Date(testForm.EliminationDate).toISOString() : new Date().toISOString(),
      SheepFilesId: testForm.SheepFilesId,
      Breed: testForm.Breed,
      House_typeId: Number(testForm.House_typeId),
      Field: Number(testForm.Field),
      LeaveStatus: Boolean(testForm.LeaveStatus),
      EliminationReason: testForm.EliminationReason,
      Handling_measuresId: Number(testForm.Handling_measuresId),
      SheepTypeId: Number(testForm.SheepTypeId)
    }
    
    requestData.value = JSON.stringify(data, null, 2)
    
    const url = '/api/CullingManagement/Update/update'
    console.log('更新URL:', url)
    console.log('请求数据:', data)
    
    const res = await Sheep_BreedingWrite.put(url, data)
    responseData.value = JSON.stringify(res.data, null, 2)
    
    if (res.data && res.data.code === 200) {
      ElMessage.success('更新成功')
    } else {
      ElMessage.error(res.data.msg || '更新失败')
    }
  } catch (error: any) {
    console.error('更新失败:', error)
    responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('更新失败')
  }
}

const testAdd = async () => {
  try {
    const data = {
      Id: 0,
      EliminationDate: testForm.EliminationDate ? new Date(testForm.EliminationDate).toISOString() : new Date().toISOString(),
      SheepFilesId: testForm.SheepFilesId,
      Breed: testForm.Breed,
      House_typeId: Number(testForm.House_typeId),
      Field: Number(testForm.Field),
      LeaveStatus: Boolean(testForm.LeaveStatus),
      EliminationReason: testForm.EliminationReason,
      Handling_measuresId: Number(testForm.Handling_measuresId),
      SheepTypeId: Number(testForm.SheepTypeId)
    }
    
    requestData.value = JSON.stringify(data, null, 2)
    
    const url = '/api/CullingManagement/Add/add'
    console.log('新增URL:', url)
    console.log('请求数据:', data)
    
    const res = await Sheep_BreedingWrite.post(url, data)
    responseData.value = JSON.stringify(res.data, null, 2)
    
    if (res.data && res.data.code === 200) {
      ElMessage.success('新增成功')
    } else {
      ElMessage.error(res.data.msg || '新增失败')
    }
  } catch (error: any) {
    console.error('新增失败:', error)
    responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('新增失败')
  }
}
</script>

<style scoped>
.test-container {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.test-card {
  margin-bottom: 20px;
}

.result-card {
  margin-bottom: 20px;
}

.button-group {
  margin-top: 20px;
  text-align: center;
}

.button-group .el-button {
  margin: 0 10px;
}

.result-content {
  background: #f8f9fa;
  padding: 15px;
  border-radius: 4px;
}

.result-content pre {
  background: #fff;
  padding: 10px;
  border-radius: 4px;
  border: 1px solid #e9ecef;
  overflow-x: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.result-content h4 {
  margin: 10px 0 5px 0;
  color: #495057;
}
</style> 